﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using AnguaRPGEngine.Library.Systems;
using AnguaRPGEngine.Library.Dice;

namespace AnguaRPGEngine.Library.Controls
{
    public partial class DiceBag : UserControl
    {
        public delegate void CurrentChangeEventHandler();
        public event CurrentChangeEventHandler OnCurrentChanged;
        public delegate void DieAddedEventHandler(IDie die);
        public event DieAddedEventHandler OnDieAdded;
        private enum DicePanelStates
        {
            Open,
            Closed,
            Opening,
            Closing
        }
        private DicePanelStates _DicePanelState = DicePanelStates.Closed;
        private const int NONE = 0;
        private const int FOUR_SIDED_DIE = 4;
        private const int SIX_SIDED_DIE = 6;
        private const int EIGHT_SIDED_DIE = 8;
        private const int TEN_SIDED_DIE = 10;
        private const int TWELVE_SIDED_DIE = 12;
        private const int TWENTY_SIDED_DIE = 20;
        private const int ONEHUNDRED_SIDED_DIE = 100; 
        private int _Hover;
        public D4 d4 = new D4();
        public D6 d6 = new D6();
        public D8 d8 = new D8();
        public D10 d10 = new D10();
        public D12 d12 = new D12();
        public D20 d20 = new D20();
        public D100 d100 = new D100();
        public DiceBag()
        {
            InitializeComponent();
        }

        private void DieMouseEnter(object sender, EventArgs e)
        {
            Cursor = Cursors.Hand;
            switch (((Panel)sender).Name)
            {
                case "btnD4":
                    _Hover = FOUR_SIDED_DIE;
                    break;
                case "btnD6":
                    _Hover = SIX_SIDED_DIE;
                    break;
                case "btnD8":
                    _Hover = EIGHT_SIDED_DIE;
                    break;
                case "btnD10":
                    _Hover = TEN_SIDED_DIE;
                    break;
                case "btnD12":
                    _Hover = TWELVE_SIDED_DIE;
                    break;
                case "btnD20":
                    _Hover = TWENTY_SIDED_DIE;
                    break;
                case "btnD100":
                    _Hover = ONEHUNDRED_SIDED_DIE;
                    break;
            }
            ((Panel)sender).BorderStyle = BorderStyle.FixedSingle;
            ((Panel)sender).BackColor = Color.Yellow;
        }

        private void DieMouseLeave(object sender, EventArgs e)
        {
            _Hover = NONE;
            Cursor = Cursors.Default;
            ((Panel)sender).BorderStyle = BorderStyle.None;
            ((Panel)sender).BackColor = SystemColors.Control;
           
        }

        private void SpinTimer_Tick(object sender, EventArgs e)
        {
            switch (_Hover)
            {
                case FOUR_SIDED_DIE:
                    d4.Roll();
                    btnD4.BackgroundImage = d4.Image;
                    break;
                case SIX_SIDED_DIE:
                    d6.Roll();
                    btnD6.BackgroundImage = d6.Image;
                    break;
                case EIGHT_SIDED_DIE:
                    d8.Roll();
                    btnD8.BackgroundImage = d8.Image;
                    break;
                case TEN_SIDED_DIE:
                    d10.Roll();
                    btnD10.BackgroundImage = d10.Image;
                    break;
                case TWELVE_SIDED_DIE:
                    d12.Roll();
                    btnD12.BackgroundImage = d12.Image;
                    break;
                case TWENTY_SIDED_DIE:
                    d20.Roll();
                    btnD20.BackgroundImage = d20.Image;
                    break;
                case ONEHUNDRED_SIDED_DIE:
                    d100.Roll();
                    btnD100.BackgroundImage = d100.Image;
                    break;
            }
            if (_DicePanelState == DicePanelStates.Opening)
            {
                int x = pnlDice.Width;
                x += 50;
                if (x >= this.Width)
                {
                    x = this.Width;
                    _DicePanelState = DicePanelStates.Open;
                }
                pnlDice.Width = x;
                pnlDice.Left = (this.Width - 1) - x;
            }
            else if (_DicePanelState == DicePanelStates.Closing)
            {
                int x = pnlDice.Width;
                x -= 50;
                if (x <= 14)
                {
                    x = 14;
                    _DicePanelState = DicePanelStates.Closed;
                }
                pnlDice.Width = x;
                pnlDice.Left = (this.Width - 1) - x;
            }
        }

        private void pnlNewDice_MouseClick(object sender, MouseEventArgs e)
        {
            if (_DicePanelState == DicePanelStates.Open)
            {
                _DicePanelState = DicePanelStates.Closing;
            }
            else if (_DicePanelState == DicePanelStates.Closed)
            {
                _DicePanelState = DicePanelStates.Opening;
            }
        }

        public void Initialize()
        {
            d4.Roll();
            btnD4.BackgroundImage = d4.Image;
            d6.Roll();
            btnD6.BackgroundImage = d6.Image;
            d8.Roll();
            btnD8.BackgroundImage = d8.Image;
            d10.Roll();
            btnD10.BackgroundImage = d10.Image;
            d12.Roll();
            btnD12.BackgroundImage = d12.Image;
            d20.Roll();
            btnD20.BackgroundImage = d20.Image;
            d100.Roll();
            btnD100.BackgroundImage = d100.Image;
            //pnlDice.Width = 14;
            //pnlDice.Left = this.Width - 15;
            cboDiceCollection.Items.Clear();
            foreach (DiceDescription dd in DieRoller.Instance.DiceCollection.Dice)
            {
                cboDiceCollection.Items.Add(dd.Name);
            }
            cboDiceCollection.SelectedIndex = cboDiceCollection.Items.IndexOf(DieRoller.Instance.DiceCollection.CurrentSet.Name);
            SpinTimer.Start();
        }

        private void cboDiceCollection_SelectedIndexChanged(object sender, EventArgs e)
        {
            DieRoller.Instance.DiceCollection.CurrentSet = DieRoller.Instance.DiceCollection.Dice.Find(p => p.Name == cboDiceCollection.Text);
            DieRoller.Instance.SaveDiceCollection();
            _DicePanelState = DicePanelStates.Closing;
            d4 = new D4();
            d6 = new D6();
            d8 = new D8();
            d10 = new D10();
            d12 = new D12();
            d20 = new D20();
            d100 = new D100();
            btnD4.BackgroundImage = d4.Image;
            btnD6.BackgroundImage = d6.Image;
            btnD8.BackgroundImage = d8.Image;
            btnD10.BackgroundImage = d10.Image;
            btnD12.BackgroundImage = d12.Image;
            btnD20.BackgroundImage = d20.Image;
            btnD100.BackgroundImage = d100.Image;
            if (OnCurrentChanged != null)
            {
                OnCurrentChanged();
            }
        }

        private void DieMouseClick(object sender, MouseEventArgs e)
        {
            if (OnDieAdded != null)
            {
                switch (((Panel)sender).Name)
                {
                    case "btnD4":
                        OnDieAdded(new D4());
                        break;
                    case "btnD6":
                        OnDieAdded(new D6());
                        break;
                    case "btnD8":
                        OnDieAdded(new D8());
                        break;
                    case "btnD10":
                        OnDieAdded(new D10());
                        break;
                    case "btnD12":
                        OnDieAdded(new D12());
                        break;
                    case "btnD20":
                        OnDieAdded(new D20());
                        break;
                    case "btnD100":
                        OnDieAdded(new D100());
                        break;
                }
            }
        }

      

       
    }
}
